package com.fly.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/3/29.
 */

public class MyView extends View {

    private Paint paintXuxian;
    private Paint paintShixian;
    private Path path;//折线路劲
    private PathEffect effect;//设置虚线

    private Shader shader;//渐变

    private int mWidth;//宽度
    private int mHeight;//高度

    public MyView(Context context) {
        super(context);
        paintXuxian = new Paint();
        paintShixian = new Paint();
        path = new Path();
        effect = new DashPathEffect(new float[]{5, 5}, 1);
        shader = new LinearGradient(0, 0, 100, 100, Color.RED, Color.BLUE, Shader.TileMode.MIRROR);
      //  effect = new DashPathEffect(new float[]{1, 2, 4, 8}, 1);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(100, 100, 20, paint);*/
      //  paint.setColor(Color.BLACK);
      //  canvas.drawColor(Color.YELLOW);
        int xEnd = getWidth() - getWidth() / 10;
        int xStart = getWidth() / 10;
        int yStart = getHeight() / 2;
        paintXuxian.setStyle(Paint.Style.STROKE);
        paintXuxian.setAntiAlias(true);//抗锯齿
        paintXuxian.setColor(Color.GRAY);//颜色
        paintXuxian.setPathEffect(effect);//虚线
        path.moveTo(xStart, yStart / 5);
        path.lineTo(xEnd, yStart / 5);//X轴
        canvas.drawPath(path, paintXuxian);

        path.moveTo(xStart, 2*yStart / 5);
        path.lineTo(xEnd, 2*yStart / 5);
        paintXuxian.setPathEffect(effect);
        canvas.drawPath(path, paintXuxian);

        path.moveTo(xStart, 3*yStart / 5);
        path.lineTo(xEnd, 3*yStart / 5);
        paintXuxian.setPathEffect(effect);
        canvas.drawPath(path, paintXuxian);

        path.moveTo(xStart, 4*yStart / 5);
        path.lineTo(xEnd, 4*yStart / 5);
        paintXuxian.setPathEffect(effect);
        canvas.drawPath(path, paintXuxian);

        path.moveTo(xStart, yStart);
        path.lineTo(xEnd, yStart);
        paintXuxian.setPathEffect(effect);
        canvas.drawPath(path, paintXuxian);

        path.moveTo(xStart, yStart / 5);
        path.lineTo(xStart, yStart);
    //    paint.setPathEffect(null);
        canvas.drawPath(path, paintXuxian);

        path.moveTo(xStart, yStart);
        path.lineTo(xEnd, yStart / 5);
        paintXuxian.setShader(shader);
        canvas.drawPath(path, paintXuxian);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /*int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        }else if(widthMode == MeasureSpec.AT_MOST){
            throw new IllegalArgumentException("width must be EXACTLY,you should set like android:width=\"200dp\"");
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        }else if(widthMeasureSpec == MeasureSpec.AT_MOST){

            throw new IllegalArgumentException("height must be EXACTLY,you should set like android:height=\"200dp\"");
        }

        setMeasuredDimension(mWidth, mHeight);*/
    }
}
